import Page from '../../common/page';
import Dialog from '../../dist/dialog/dialog';
import Notify from '../../dist/notify/notify';

const request = require('../../utils/https.js')
var userId;

Page({


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    userId = wx.getStorageSync("userId")

    console.log('options===>', options)
    let address = options.address //0是地址管理进入
    let type = options.type //0 订单提交进入 1.我的页面进入
    that.setData({
      address: address,
      type: type,
      tradeType: -1,
    })

  },
  //
  goMail(e) {
    const that = this;
    // addressId有值为编辑 空值为新增
    let addressId = e.currentTarget.dataset.addressid;
    wx.navigateTo({
      url: '/pages/mail/mail?addressId=' + addressId,
    })
  },
  goBack(e) {
    // addressId有值为编辑 空值为新增
    let addressId = e.currentTarget.dataset.addressid;
    wx.navigateBack({
      delta: 1
    })

    wx.setStorageSync('addressId', addressId)

  },
  //删除单条商品
  onClose(event) {
    const that = this;
    let itemid = event.currentTarget.dataset.itemid;
    const {
      position,
      instance
    } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？'
        }).then(() => {
          instance.close();
          //请求删除接口
          that.onDelete(itemid, that);
        });
        break;
    }
  },

  //删除地址
  onDelete(itemid, that) {
    request._get(getApp().Api_url + 'userAddress/del', {
      userId: userId,
      addressId: itemid
    }, function (res) {
      if (res.data.errCode == 200) {
        //添加购物车成功
        //根据接口返回是否添加成功 成功提醒
        Notify({
          duration: 1000,
          text: '删除成功~',
          selector: '#custom-selector',
          backgroundColor: '#e92f2e',
          safeAreaInsetTop: true
        });
        that.onShow();
      }
    }, function () {})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    request._get(getApp().Api_url + 'userAddress/mine', { //地址列表接口
      userId: userId,
      type: that.data.type,
      tradeType: that.data.tradeType
    }, function (res) {
      if (res.data.errCode == 200) {
        let addressList = res.data.data;
        that.setData({
          addressList
        })

      }
    }, function () {});
  },
  chooseAddress() {
    const that = this;
    wx.chooseAddress({
      success: (res) => {

        if (res.errMsg == 'chooseAddress:ok') {
          console.log('res===>', res)
          let provinceName = res.provinceName;
          let cityName = res.cityName;
          let countyName = res.countyName;
          let detailInfo = res.detailInfo;
          let nationalCode = (res.nationalCode).substring(0, 2) + '0000';
          let userName = res.userName;
          let telNumber = res.telNumber;
          console.log('nationalCode==>', nationalCode)
          that.postSubmit(provinceName, cityName, countyName, detailInfo, nationalCode, userName, telNumber)
        }


      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  postSubmit(province, city, town, others, code, username, mobile) {
    const that = this;
    var isDefault = 0;
    var addressId;
    console.log('code==>', code)
    console.log('province==>', province)
    console.log('city==>', city)
    console.log('town==>', town)
    console.log('others==>', others)
    console.log('mobile==>', mobile)
    console.log('username==>', username)
    console.log('isDefault==>', isDefault)
    console.log('that.data.addressList==>', that.data.addressList)

    if (that.data.addressList.length > 0) {
      for (var v = 0; v < that.data.addressList.length; v++) {
        if (province == that.data.addressList[v].province && city == that.data.addressList[v].city && town == that.data.addressList[v].town && others == that.data.addressList[v].others && username == that.data.addressList[v].userName && mobile == that.data.addressList[v].mobile) {
          addressId = that.data.addressList[v].addressId;
          break;
        } else {
          addressId = '';
        }
      }
    } else {
      addressId = '';
    }



    //判断信息填写完整没有
    request._post(getApp().Api_url + 'userAddress/add', { //添加地址
      userId: userId,
      userName: username,
      code: code,
      province: province,
      city: city,
      town: town,
      others: others,
      mobile: mobile,
      isDefault: isDefault,
      addressId: addressId
    }, function (res) {

      console.log('提交地址===>', res)

      if (res.data.errCode == 200) {
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          duration: 2000
        })
      }
    }, function () {})


  },

})